#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include <sys/system_properties.h>

static void getprop(const char * prop_cmd, char result[], const char * def) {
	if (!prop_cmd || !result) return;
	bzero(result, sizeof(result));
	if (0 == __system_property_get(prop_cmd, result)) {
		memcpy(result, def, strlen(def));
	}
}

static void getprop_int(const char * prop_cmd, int &result, int def) {
	if (!prop_cmd) return;
	char tmp[64] = {0};
	if (0 == __system_property_get(prop_cmd, tmp)) {
		result = def;
	} else {
		result = atoi(tmp);
	}
}

static void setprop(const char * prop_cmd, char result[]) {
	if (!prop_cmd || !result) return;
	__system_property_set(prop_cmd, result);
}


static void setprop_int(const char * prop_cmd, int result) {
	char value[64] = {0};
	/* itoa(result, value, 10); */
	sprintf(value, "%d", result);
	setprop(prop_cmd, value);
}

static void delprop(const char * prop_cmd) {
	if (!prop_cmd) return;
	__system_property_set(prop_cmd, "");
}

struct ocfg_opt_t config_uci_opt = {
	.ocfg_get = getprop,
	.ocfg_get_int = getprop_int,
	.ocfg_set = setprop,
	.ocfg_set_int = setprop_int,
	.ocfg_del = delprop,
};
